﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Solution.Business.Component;
using Solution.Business.Entities;
using Solution.Data;
using System.Data;

namespace Solution.UI.Web.Controllers.Admin
{
    public class AdminLocationController : Controller
    {
        private SolutionEntities db = new SolutionEntities();
        //
        // GET: /AdminLocation/

        public ActionResult Index()
        {
            var list = db.Locations.OrderBy(c => c.LId);
            return View(list.ToList());
        }

        public ActionResult Details(int id = 0)
        {
            Location location = db.Locations.Find(id);
            if (location == null)
            {
                return HttpNotFound();
            }
            return View(location);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Location location)
        {
            if (ModelState.IsValid)
            {
                if (location.Name != null && location.Level != null && location.LGroup != null)
                {
                    db.Locations.Add(location);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError("", "Bạn chưa nhập đầy đủ thông tin trong các ô có dấu * !");
                }
            }
            return View(location);
        }

        public ActionResult Edit(int id = 0)
        {
            Location location = db.Locations.Find(id);
            if (location == null)
            {
                return HttpNotFound();
            }
            return View(location);
        }

        [HttpPost]
        public ActionResult Edit(Location location)
        {
            if (ModelState.IsValid)
            {
                db.Entry(location).State = EntityState.Modified;
                if (location.Name != null && location.Level != null && location.LGroup != null)
                {
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError("", "Bạn chưa nhập đầy đủ thông tin trong các ô có dấu * !");
                }
            }
            return View(location);
        }

        public ActionResult Delete(int id = 0)
        {
            Location location = db.Locations.Find(id);
            if (location == null)
            {
                return HttpNotFound();
            }
            return View(location);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var location = db.Locations.Find(id);
            db.Locations.Remove(location);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
